package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.Painter;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompConector._arrowType;

import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;

public class CompNodo extends Componente {

	static int MaxCharacterPerLine = 10;

	private int _radius;
	private ArrayList<String> TextLine = new ArrayList<String>();
	private String longestString = "Nodo";
	private Point textPoint;
	private int textLineSpan;
	
	private float distCost;
	private CompConector prevNodePath;
	private boolean isVisited;

	private float SquareCase = 0;
	private float TriangleCase = 0;
	private boolean showPert = false;
	
	public CompNodo() {
		_centerPoint = new Point(0, 0);
		textPoint = new Point();
		textLineSpan = 0;
	}

	@Override
	public void setTitulo(String titulo) {
		Titulo = titulo;
		TextLine = DividirTitulo(titulo);
		int aux;
		for (String value : TextLine) {
			aux = value.length();
			if (aux > longestString.length()) {
				longestString = value;
			}
		}
		_width = Painter.getInstancia().getTextPaint(false, 1)
				.measureText(longestString);
		_height = _width;
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
			int offSetX, int offSetY) {

		int posX = _centerPoint.x + offSetX;
		int posY = _centerPoint.y + offSetY;
		int textWidht;
		// Dibujamos el texto
		for (int i = 0; i < TextLine.size(); i++) {
			textWidht = (int) Painter.getInstancia()
					.getTextPaint(false, globalScale)
					.measureText(TextLine.get(i));
			c.drawText(TextLine.get(i), ((posX + 2) * globalScale)
					- (textWidht / 2), ((textPoint.y + offSetY)
					- (textLineSpan * (TextLine.size() - i - 1)) - 2)
					* globalScale,
					Painter.getInstancia().getTextPaint(false, globalScale));
		}

		// Dibujamos el contorno
		// _radius =(int) Painter.getInstancia().getTextPaint(false,
		// globalScale).measureText(longestString);
		_radius = 8 + (int) (_width / 2);
		c.drawCircle(posX * globalScale, posY * globalScale, _radius
				* globalScale,
				Painter.getInstancia().getBorderPaint(_currentState));

		// Point z = calcularPuntoExterno(-0.5);
		// c.drawCircle(z.x, z.y, 10,
		// Painter.getInstancia().getArrowPaint(_currentState, true));

		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}
		
		//Dibujar los elementos del PertCpm
		
		if(showPert){
			
			//Dibujo del cuadro
			Path square = new Path();
			square.moveTo((_centerPoint.x+(_radius/2)+ offSetX-2)*globalScale,
					      (_centerPoint.y- _radius+offSetY-5)* globalScale);
			square.lineTo((_centerPoint.x+(_radius/2)+ offSetX-22)*globalScale,
					      (_centerPoint.y- _radius+offSetY-5)* globalScale);
			square.lineTo((_centerPoint.x+(_radius/2)+ offSetX-22)*globalScale,
				          (_centerPoint.y- _radius+offSetY-25)* globalScale);
			square.lineTo((_centerPoint.x+(_radius/2)+ offSetX-2)*globalScale,
			              (_centerPoint.y- _radius+offSetY-25)* globalScale);
			square.close();
			
			c.drawPath(square, Painter.getInstancia().getBlockPaint(CompObjeto.IDLE, false));
			
			if((int)SquareCase == SquareCase ){
				c.drawText("="+Integer.toString((int)SquareCase),
						(_centerPoint.x+(_radius/2)+ offSetX)*globalScale,
						(_centerPoint.y- _radius+offSetY-5)
								* globalScale,
						Painter.getInstancia().getTextPaint(false, globalScale));
			}else{
				c.drawText("="+Float.toString(SquareCase),
						(_centerPoint.x+(_radius/2)+ offSetX)*globalScale,
						(_centerPoint.y- _radius+offSetY-5)
								* globalScale,
						Painter.getInstancia().getTextPaint(false, globalScale));
			}
			
			
			//Dibujo del triangulo
			
			Path triangle = new Path();
			triangle.moveTo((_centerPoint.x+(_radius/2)+ offSetX-2)*globalScale,
					      (_centerPoint.y- _radius+offSetY-40)* globalScale);
			triangle.lineTo((_centerPoint.x+(_radius/2)+ offSetX-22)*globalScale,
					      (_centerPoint.y- _radius+offSetY-40)* globalScale);
			triangle.lineTo((_centerPoint.x+(_radius/2)+ offSetX-12)*globalScale,
				          (_centerPoint.y- _radius+offSetY-55)* globalScale);
			triangle.close();
			
			c.drawPath(triangle, Painter.getInstancia().getBlockPaint(CompObjeto.IDLE, false));
			
			
			if((int)TriangleCase == TriangleCase ){
				c.drawText("="+Integer.toString((int)TriangleCase),
						(_centerPoint.x+(_radius/2)+ offSetX)*globalScale,
						(_centerPoint.y- _radius+offSetY-35)
								* globalScale,
						Painter.getInstancia().getTextPaint(false, globalScale));
			}else{
				c.drawText("="+Float.toString(TriangleCase),
						(_centerPoint.x+(_radius/2)+ offSetX)*globalScale,
						(_centerPoint.y- _radius+offSetY-35)
								* globalScale,
						Painter.getInstancia().getTextPaint(false, globalScale));
			}
		}
		

	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,
			float GlobalScale) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		int CompX = _centerPoint.x - touch.x;
		int CompY = _centerPoint.y - touch.y;
		if ((CompX * CompX + CompY * CompY) <= (_radius * _radius)) {
			return true;
		}
		return false;
	}

	@Override
	public CompConector calcularTouchConectores(Point touch) {
		for (CompConector value : _conectorsBegining) {
			if (value.calcularTouch(touch) == true)
				return value;
		}
		return null;
	}

	@Override
	public void actualizarAchorsPoints() {
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsBegining) {
				value.CalcularConexiones(this, VistaDiagrama.getVista()
						.getDiagrama().getComponenteById(value._childId));
			}
		}
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsEnd) {
				value.CalcularConexiones(VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(value._fatherId), this);
			}
		}
	}

	@Override
	public void actualizarPuntoCentral() {
		_centerPoint.x = (int) (Anchor.x + (_width / 2));
		_centerPoint.y = (int) (Anchor.y + (_height / 2));
		Rect mBounds = new Rect();
		int TotalTextHeight = 0;
		for (int i = 0; i < TextLine.size(); i++) {
			Painter.getInstancia().getTextPaint(false, 1)
					.getTextBounds(TextLine.get(i), 0, 2, mBounds);
			TotalTextHeight += (int) ((float) mBounds.height() * 1.2);
		}
		textPoint.x = (int) (_centerPoint.x - (Painter.getInstancia()
				.getTextPaint(false, 1).measureText(longestString) / 2));
		textPoint.y = _centerPoint.y + (TotalTextHeight / 2);
		if (TextLine.size() > 0) {
			textLineSpan = TotalTextHeight / TextLine.size();
		}
	}

	@Override
	public String generarDOM() {
		String DOM = "\t<Componente Tipo=\"Nodo\" Titulo=\"" + Titulo
				+ "\" ID=\"" + ID + "\" AnchorX=\"" + Anchor.x
				+ "\" AnchorY=\"" + Anchor.y + "\" Radius=\"" + Integer.toString(_radius) + "\" >\n";

		DOM = DOM + "\t</Componente>\n";
		return DOM;
	}

	@Override
	public String getConectorsDOM() {
		String DOM = "";
		for (CompConector value : _conectorsBegining) {
			DOM = DOM + value.generarDOM();
		}
		return DOM;
	}

	@Override
	public void calculoLongitud() {
		// TODO Auto-generated method stub

	}

	@Override
	public Point requestConnectionPoint(CompConector connector, int mode,
			Point otherCompCenter) {
		float dX = (otherCompCenter.x - _centerPoint.x);
		float dY = (otherCompCenter.y - _centerPoint.y);
		if (dY == 0) {
			return otherCompCenter;
		} else {
			double m = dY / dX;
			if (_centerPoint.x > otherCompCenter.x) {
				return calcularPuntoExterno(m, true);
			} else {
				return calcularPuntoExterno(m, false);
			}
		}
	}

	public ArrayList<String> DividirTitulo(String titulo) {
		ArrayList<String> respuesta = new ArrayList<String>();
		String[] arreglo = titulo.split(" ");
		String linea = "";
		int acumulatedChar = 0;
		for (int i = 0; i < arreglo.length; i++) {
			acumulatedChar += arreglo[i].length();
			linea += arreglo[i] + " ";
			if (acumulatedChar >= MaxCharacterPerLine) {
				respuesta.add(linea);
				linea = "";
				acumulatedChar = 0;
			}
		}
		if (acumulatedChar > 0) {
			respuesta.add(linea);
		}
		return respuesta;
	}

	private Point calcularPuntoExterno(double m, boolean inverse) {
		Point Respuesta = new Point();
		double angle = Math.atan(m);
		if (inverse) {
			Respuesta.x = _centerPoint.x - (int) (_radius * Math.cos(angle));
			Respuesta.y = _centerPoint.y - (int) (_radius * Math.sin(angle));
		} else {
			Respuesta.x = _centerPoint.x + (int) (_radius * Math.cos(angle));
			Respuesta.y = _centerPoint.y + (int) (_radius * Math.sin(angle));
		}

		return Respuesta;
	}

	@Override
	public void mover(int distX, int distY) {
		Anchor.x += distX;
		Anchor.y += distY;
		actualizarPuntoCentral();
		actualizarAchorsPoints();

	}

	@Override
	public String getTipo() {
		return "Nodo";
	}

	public float getDistCost() {
		return distCost;
	}

	public void setDistCost(float distCost) {
		this.distCost = distCost;
	}

	public CompConector getPrevNodePath() {
		return prevNodePath;
	}

	public void setPrevNodePath(CompConector prevNodePath) {
		this.prevNodePath = prevNodePath;
	}

	public boolean isVisited() {
		return isVisited;
	}

	public void setVisited(boolean isVisited) {
		this.isVisited = isVisited;
	}

	public int get_radius() {
		return _radius;
	}

	public void set_radius(int _radius) {
		this._radius = _radius;
	}

	public float getSquareCase() {
		return SquareCase;
	}

	public void setSquareCase(float squareCase) {
		SquareCase = squareCase;
	}

	public float getTriangleCase() {
		return TriangleCase;
	}

	public void setTriangleCase(float triangleCase) {
		TriangleCase = triangleCase;
	}

	public void setShowPert(boolean showPert) {
		this.showPert = showPert;
	}
	
	
}
